# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/4/13
# @File   : Rule31_LoanAndCard_InspectCnt_1monCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:近1个月贷款审批机构数和信用卡审批查询次数校验
code:LoanAndCard_InspectCnt_1monCheck_2D
tableName:
columnName:
groups:准入>个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)

    if occurTimes == 1:
        if table == "":
            return ""
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def Formate(time):
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    if len(tmp) == 6:
                        date = datetime.datetime.strptime(tmp, '%Y%m').strftime('%Y-%m-%d')
                    else:
                        date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date


#查询记录概要  PQO  [1..1] 查询记录概要信息单元  PC05 [1..1]
#查询记录汇总信息段  PC05B  [1..1]
# 最近 1 个月内的查询机构（贷款审批）PC05BS01  最近 1 个月内的查询机构数（信用卡审批）PC05BS02
def LoanAndCard_InspectCnt_1monCheck():
    data = IC_DSJ_2D['data']
    PQO = getTable(data, 'PQO', occurTimes=1)
    # print PQO
    if PQO is None or PQO == [{}] or PQO == "":
        return u'缺失值'
    else:
        PC05 = getTable(PQO[0], 'PC05', 1)
        if PC05 is None or PC05 == [{}] or PC05 == "":
            return u'缺失值'
        else:
            PC05B = getTable(PC05[0], 'PC05B', 1)
            if PC05B is None or PC05B == [{}] or PC05B == "":
                return u'缺失值'
            else:
                PC05BS01 = PC05B[0].get('PC05BS01')
                PC05BS02 = PC05B[0].get('PC05BS02')
                if not isfloat(PC05BS01) and not isfloat(PC05BS02):
                    return u'缺失值'
                else:
                    try:
                        PC05BS01 = float(PC05BS01)
                    except:
                        PC05BS01 = 0
                    try:
                        PC05BS02 = float(PC05BS02)
                    except:
                        PC05BS02 = 0

                    return PC05BS01 + PC05BS02



result = LoanAndCard_InspectCnt_1monCheck()
